﻿package ${Package}.action.manage;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import ${Package}.bo.${table.TableNameAlias}Bo;
import ${Package}.domain.${table.TableNameAlias};
import ${Package}.security.Authority;
import ${Package}.security.PermissionConfig;
import ${Package}.security.PermissionItem;
import ${Package}.security.PermissionType;
import ${Package}.utils.GenericUtils;
import ${Package}.utils.OperateStatus;
import ${Package}.utils.RequestUtils;
import ${Package}.utils.ReturnResult;
import com.opensymphony.xwork2.ActionSupport;
	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:${Utils.GetDate} ${Utils.GetTime}
*/ 
public class ${table.TableNameAlias}Action extends ActionSupport {
    Logger logger = Logger.getLogger(${table.TableNameAlias}Action.class);
	private static final String MODULECODE = "${table.TableNameAlias}";
    public ${table.TableNameAlias}Action() {
    }

    /*添加数据*/
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.Add)
    public void Add() {
		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
		#if($c.ColumnType=="Integer")
        $c.ColumnType $columnName =RequestUtils.getInteger("${c.ColumnNameAlias}",0);
		#elseif($c.ColumnType=="Date")
		 #if((${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
		   $c.ColumnType $columnName =new Date(RequestUtils.getString("${c.ColumnNameAlias}"));
		  #end
		#elseif($c.ColumnType=="Date"&&(${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
				$c.ColumnType $columnName = new Date();
				try {
					$columnName = new Date(RequestUtils.getString("${c.ColumnNameAlias}"));
				} catch (ParseException e) {
					e.printStackTrace();  
				}
		#else
		$c.ColumnType $columnName =RequestUtils.getString("${c.ColumnNameAlias}");
		#end
		#end
        ${table.TableNameAlias} entity = new ${table.TableNameAlias}();
        Date date = new Date();
		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
			#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
			  #if((${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
		         entity.set${c.ColumnNameAlias}($columnName);
			   #else
			     entity.set${c.ColumnNameAlias}(date);
			   #end
            #end
		#end
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.Add(entity);
		GenericUtils.outResult(result);
    }
	    /*
    * 删除数据
    * */
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.Delete)
    public void Delete() {
		String ids = RequestUtils.getString("ids");
		if (!"".equals(ids)) {
		String[] idArr = ids.split(",");
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.Delete(idArr);
		GenericUtils.outResult(result);
		}
    }

	    /*修改数据*/
		 @Authority(moduleCode =MODULECODE,permissionType = PermissionType.Edit)
    public void Edit() {

		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
		#if($c.ColumnType=="Integer")
        $c.ColumnType $columnName =RequestUtils.getInteger("${c.ColumnNameAlias}",0);
		#elseif($c.ColumnType=="Date")
		 #if((${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
		   $c.ColumnType $columnName =new Date(RequestUtils.getString("${c.ColumnNameAlias}"));
		  #end
		#elseif($c.ColumnType=="Date"&&(${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
				$c.ColumnType $columnName = new Date();
				try {
					$columnName = new Date(RequestUtils.getString("${c.ColumnNameAlias}"));
				} catch (ParseException e) {
					e.printStackTrace();  
				}
		#else
		$c.ColumnType $columnName =RequestUtils.getString("${c.ColumnNameAlias}");
		#end
		#end
        ${table.TableNameAlias} entity =new ${table.TableNameAlias}();
        Date date = new Date();
        #foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
		   #if(${c.ColumnName}!="Create_Date")
			  #if(${c.ColumnName}!="Update_Date")
				 entity.set${c.ColumnNameAlias}($columnName);	
			   #else
					entity.set${c.ColumnNameAlias}(date);
			   #end
			#end
        #end
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.Edit(entity);
		GenericUtils.outResult(result);
    }
    /*数据列表*/
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.List)
    public void List() {
		int pageIndex = RequestUtils.getInteger("page", 0);
		int pageSize = RequestUtils.getInteger("rows", 0);
		String title = RequestUtils.getString("SearchKey");
		int state = RequestUtils.getInteger("state", -1);
		Map<String, Object> map=new HashMap<String, Object>();
		if (state>-1) {
			map.put("status", state);	
		}
		if (!"".equals(title)) {
			map.put("title", title);
		}
		${table.TableNameAlias}Bo bo=new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.List(pageIndex,pageSize,map);
		GenericUtils.outResult(result);
        
    }
	    /*
    * 详细数据信息
    * */
	 @Authority(moduleCode =MODULECODE,permissionType = PermissionType.View)
    public void View() {
		int id = RequestUtils.getInteger("${primaryKeyAlias}", 0);
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.View(id);
		GenericUtils.outResult(result);
    }
	 /*
    * 更新状态
    * */
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.On)
    public void Audit() {
		String ids = RequestUtils.getString("ids");
		int status = RequestUtils.getInteger("status", -1);
		if (!"".equals(ids)) {
			String[] idArr = ids.split(",");
			${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
			ReturnResult result = bo.Audit(idArr, status);
			GenericUtils.outResult(result);
		}        
    }
 /*
    * 当前用户操作权限
    * */
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    public void Permission() {
        PermissionConfig config = new PermissionConfig();
        try {
            List<PermissionItem> lst = config.getPermissionItems(MODULECODE);
            OperateStatus operateStatus = new OperateStatus();
            operateStatus.setMsg("返回列表成功");
            operateStatus.setStatus(OperateStatus.Status.SUCCESS);
            ReturnResult result = new ReturnResult();
            result.setDataState(operateStatus);
            result.setRows(lst);
            GenericUtils.outResult(result);           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
